package jgcp.client;

import javax.swing.JOptionPane;

import jgcp.client.CmdClient.STATUS;
import jgcp.common.network.Connection;
import jgcp.common.network.FileReceiver;
import jgcp.message.Message;
import jgcp.message.MessageHandler;
import jgcp.message.MessageType;

public class GUiClientMessageHandler extends MessageHandler {
	GuiClient guiRef = null;
	public GUiClientMessageHandler(Connection con) {
		super(con);
		setDaemon(true);
	}
	void setGui(GuiClient gc)
	{
		guiRef = gc;
	}
	
	public void processMessage(Message m) throws Exception {
		if (m.getType() == MessageType.LOGIN_SUCCESS) {
			guiRef.isConnected = true;
			guiRef.enableTabs();
			
//			CmdClient.status = STATUS.CONNECTED;
//			System.out.println(m.getMessageBody());
		}
		else if(m.getType() == MessageType.LOGIN_REJECT)
		{
			guiRef.isConnected = false;
			JOptionPane.showMessageDialog(null, m.getMessageBody());
//			CmdClient.status = STATUS.NOT_CONNECTED;
//			System.out.println(m.getMessageBody());
		}
		else if(m.getType() == MessageType.GETRESULT_READY)
		{
			String hostname = connection.getSocket().getInetAddress().getHostAddress();
			int port = Integer.parseInt(m.getMessageBody());
			FileReceiver<String> fr = new FileReceiver<String>(hostname,port,guiRef.getResultFile());
			fr.start();
		}
		else if(m.getType() == MessageType.GETRESULT_NOTREADY)
		{
			JOptionPane.showMessageDialog(null, m.getMessageBody());
		}
		else if(m.getType() == MessageType.LISTTASKS_RESPONSE)
		{
			guiRef.setTasklist(m.getMessageBody());
		}
		else if(m.getType() == MessageType.LISTWORKER_RESPONSE)
		{
			guiRef.setWrokers(m.getMessageBody());
		}
		else
		{
			JOptionPane.showMessageDialog(null, m.getMessageBody());
		}
	}
}
